﻿<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:System_Windows_Controls_Customs="clr-namespace:System.Windows.Controls.Customs;assembly=System.Windows.Controls.Customs"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:Sora_HelpSystem_Editor_Converters="clr-namespace:Sora.HelpSystem.Editor.Converters"
    xmlns:Sora_HelpSystem_Editor_Validators="clr-namespace:Sora.HelpSystem.Editor.Validators"
    x:Name="window" mc:Ignorable="d" x:Class="Sora.HelpSystem.Editor.MainWindow"
        Title="Sora Help Editor" Height="350" Width="525">
    <Window.Resources>
        <ResourceDictionary>
        	<Sora_HelpSystem_Editor_Converters:EditablePageItemWrapperConverter x:Key="EditablePageItemWrapperConverter"/>
        	<Sora_HelpSystem_Editor_Converters:NotNullToEnabledConverter x:Key="NotNullToEnabledConverter"/>
        	<Sora_HelpSystem_Editor_Converters:HelpPageTitleConverter x:Key="HelpPageTitleConverter"/>
            <HierarchicalDataTemplate x:Key="ContentTreeTemplate" ItemsSource="{Binding Children}">
                <Grid Name="grid">
                    <TextBlock DataContext="{Binding DataContext, Converter={StaticResource EditablePageItemWrapperConverter}, ElementName=grid}" Text="{Binding Page, Converter={StaticResource HelpPageTitleConverter}, Mode=OneWay}"/>
                </Grid>
            </HierarchicalDataTemplate>

            <ControlTemplate x:Key="validationTemplate">
                <DockPanel>
                    <TextBlock Foreground="Red" FontSize="20">!</TextBlock>
                    <AdornedElementPlaceholder/>
                </DockPanel>
            </ControlTemplate>

            <Style x:Key="textStyleTextBox" TargetType="TextBox">
                <Setter Property="Foreground" Value="#333333" />
                <Setter Property="MaxLength" Value="40" />
                <Setter Property="Width" Value="392" />
                <Style.Triggers>
                    <Trigger Property="Validation.HasError" Value="true">
                        <Setter Property="ToolTip"
                        Value="{Binding RelativeSource={RelativeSource Self},
                        Path=(Validation.Errors)[0].ErrorContent}"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </ResourceDictionary>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="20" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200" />
            <ColumnDefinition Width="5" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        
        <Menu Grid.ColumnSpan="3">
            <MenuItem Header="File">
                <MenuItem Header="New" Name="menuNew"/>
                <MenuItem Header="Open" Name="menuOpen" Click="menuOpen_Click"/>
                <Separator/>
                <MenuItem Header="Save" Name="menuSave" Click="menuSave_Click"/>
                <MenuItem Header="Save as ..." Name="menuSaveAs" Click="menuSaveAs_Click"/>
                <Separator/>
                <MenuItem Header="Quit" Name="menuQuit"/>
            </MenuItem>
            <MenuItem Header="Tools">
                <MenuItem Header="Generate Help Search Index" Name="menuGenerateSearch" Click="menuGenerateSearch_Click"/>
            </MenuItem>
            <MenuItem Header="Help"></MenuItem>
        </Menu>

        <TreeView x:Name="treeView" BorderBrush="{x:Null}" ItemsSource="{Binding EditorRoot.HelpRoot.Children, ElementName=window, Mode=OneWay}" ItemTemplate="{StaticResource ContentTreeTemplate}" Grid.Row="1" ContextMenuOpening="treeView_ContextMenuOpening" MouseDown="treeView_MouseDown">
            <TreeView.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="New sub page" Name="menuTreeNewSubPage" Click="menuTreeNewSubPage_Click"/>
                    <MenuItem Header="New page at the same level" Name="menuTreeNewLevelPage" Click="menuTreeNewLevelPage_Click"/>
                    <Separator/>
                    <MenuItem Header="Delete page" Name="menuTreeDeletePage" Click="menuTreeDeletePage_Click"/>
                </ContextMenu>
            </TreeView.ContextMenu>
        </TreeView>

        <GridSplitter Grid.Column="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Background="White" Grid.Row="1" />
        <Grid Grid.Column="2" IsEnabled="{Binding SelectedItem, Converter={StaticResource NotNullToEnabledConverter}, ElementName=treeView, Mode=OneWay}" Grid.Row="1">
        	<Grid.ColumnDefinitions>
        		<ColumnDefinition Width="100"/>
        		<ColumnDefinition Width="*"/>
        	</Grid.ColumnDefinitions>
        	<Grid.RowDefinitions>
        		<RowDefinition Height="25"/>
                <RowDefinition Height="25"/>
                <RowDefinition Height="25"/>
                <RowDefinition Height="25"/>
                <RowDefinition Height="*"/>
        	</Grid.RowDefinitions>

            <System_Windows_Controls_Customs:TextEditor x:Name="contentEditor" d:LayoutOverrides="Width, Height" Grid.Row="4" Grid.ColumnSpan="2" Document="{Binding SelectedItem.FlowContent.Source, ElementName=treeView, Mode=TwoWay}" SpellCheckIsEnabled="True" />

            <TextBlock TextWrapping="Wrap" Text="Title :" d:LayoutOverrides="Width" VerticalAlignment="Center"/>
        	<TextBox Grid.Column="1" TextWrapping="Wrap" Text="{Binding SelectedItem.PageTitle, ElementName=treeView, Mode=TwoWay}" VerticalAlignment="Center" Margin="0,0,5,0"/>
        	
            <TextBox Grid.Column="1" Grid.Row="1" TextWrapping="Wrap" Text="{Binding SelectedItem.Uri, ElementName=treeView, Mode=OneWay}" d:LayoutOverrides="Height" VerticalAlignment="Center" Margin="0,0,5,0" IsReadOnly="True"/>
        	<TextBlock Grid.Row="1" TextWrapping="Wrap" Text="Uri :" d:LayoutOverrides="Width, Height" VerticalAlignment="Center"/>

            <TextBox Grid.Column="1" Grid.Row="2" TextWrapping="Wrap" d:LayoutOverrides="Height" VerticalAlignment="Center" Margin="0,0,5,0"
                     Validation.ErrorTemplate="{StaticResource validationTemplate}" 
                     Style="{StaticResource textStyleTextBox}" Width="Auto">
                <TextBox.Text>
                    <Binding ElementName="treeView" Mode="TwoWay" Path="SelectedItem.Name" ValidatesOnDataErrors="True" ValidatesOnExceptions="True">
                        <Binding.ValidationRules>
                            <Sora_HelpSystem_Editor_Validators:PageNameValidationRule />
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox.Text>
            </TextBox>
            
            <TextBlock Grid.Row="2" TextWrapping="Wrap" Text="Name :" d:LayoutOverrides="Width, Height" VerticalAlignment="Center"/>

            <CheckBox Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="3" Content="Displayed in the summary" IsChecked="{Binding SelectedItem.IsSummaryVisible, ElementName=treeView, Mode=TwoWay}" d:LayoutOverrides="Height" VerticalAlignment="Center" Margin="0,0,5,0"/>
        </Grid>
    </Grid>
</Window>
